package com.yetianhua.consistencysession.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author yetianhua
 * @className LoginController
 * @description TODO
 * @date 2021/7/25
 * @time 13:28
 */
@Controller
public class LoginController {

    @ResponseBody
    @GetMapping("/test")
    public String test() {
        return "hello world";
    }

    @GetMapping("/toLogin")
    public String  toLogin(){
        return "login";
    }

    @PostMapping("/loginSystem")
    public String loginSystem(String username, String password, HttpSession session){
        // 合法用户，信息写入session，同时跳转到系统主页面
        if("admin".equals(username) && "admin".equals(password)) {
            System.out.println("合法用户");
            session.setAttribute("username",username + System.currentTimeMillis());
            return "redirect:result";
        }else{
            // 非法用户返回登录页面
            System.out.println("非法，跳转");
            return "redirect:toLogin";
        }
    }

}
